﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web;
using System.Security.Permissions;
using System.Web.UI.WebControls;

[assembly: System.Web.UI.WebResource("Batik.Web.UI.Controls.DataView.Script.js", "application/x-javascript")]

namespace Batik.Web.UI.Controls
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [ToolboxData("<{0}:DataView runat=server></{0}:DataView>")]
    public class DataView : ScriptControl
    {
        public object DataSource { get; set; }

        [Bindable(false), Description(""), Category("Data"), DefaultValue("ClientBinding")]
        public BindingMode BindingMode { get; set; }
        
        public string DataSourceID { get; set; }
        public virtual string Data { get; private set; }
        public virtual string DataProvider { get; private set; }
        public virtual string FetchOperation { get; private set; }
        public virtual string FetchParameters { get; private set; }
        [Bindable(false), Category("Data"), DefaultValue("true")]
        public bool AutoFetch { get; set; }

        [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), DefaultValue((string)null), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Style HeaderStyle { get; set; }
        [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(DataViewItem)), TemplateInstance(TemplateInstance.Multiple)]
        public ITemplate HeaderTemplate { get; set; }
        [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(DataViewItem)), TemplateInstance(TemplateInstance.Multiple)]
        public ITemplate ItemTemplate { get; set; }
        [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(DataViewItem)), TemplateInstance(TemplateInstance.Multiple)]
        public ITemplate ProgressIndicatorTemplate { get; set; }
        [DefaultValue(false)]
        public bool RenderTable { get; set; }
        [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(DataViewItem)), TemplateInstance(TemplateInstance.Multiple)]
        public ITemplate EditTemplate { get; set; }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
            if (RenderTable)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "1px");
                writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "1px");
                writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#dedede");
                writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
                writer.RenderBeginTag(HtmlTextWriterTag.Table);
            }
            else
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                //base.RenderBeginTag(writer);
            }
        }
        protected override void CreateChildControls()
        {
            //base.CreateChildControls();
            int count = 0;
            if (HeaderTemplate != null)
            {
                // create instance of DataViewItem control
                DataViewItem header = new DataViewItem(DataSource, count++);
                // instantiate in new item object
                header.ID = "HeaderTemplate";
                header.IsClientTemplate = true;
                HeaderTemplate.InstantiateIn(header);
                if (RenderTable)
                {
                    header.TemplateContainerTag = HtmlTextWriterTag.Thead;
                }
                else
                {
                    header.TemplateContainerTag = HtmlTextWriterTag.Div;
                }
                // add item to Controls collection
                Controls.Add(header);
                // need to support <%# %> expressions
                header.DataBind();
            }
            if (ItemTemplate != null)
            {
                // create instance of DataViewItem control
                DataViewItem item = new DataViewItem(DataSource, count++);
                // instantiate in new item object
                item.ID = "ItemTemplate";
                item.IsClientTemplate = true;
                ItemTemplate.InstantiateIn(item);
                if (RenderTable)
                {
                    item.TemplateContainerTag = HtmlTextWriterTag.Tbody;
                }
                else
                {
                    item.TemplateContainerTag = HtmlTextWriterTag.Div;
                }
                // add item to Controls collection
                Controls.Add(item);
                // need to support <%# %> expressions
                item.DataBind();
            }
            if (EditTemplate != null)
            {
                // create instance of DataViewItem control
                DataViewItem edit = new DataViewItem(DataSource, count++);
                // instantiate in new item object
                edit.ID = "EditTemplate";
                edit.IsClientTemplate = true;
                EditTemplate.InstantiateIn(edit);
                if (RenderTable)
                {
                    edit.TemplateContainerTag = HtmlTextWriterTag.Tbody;
                }
                else
                {
                    edit.TemplateContainerTag = HtmlTextWriterTag.Div;
                }
                // add item to Controls collection
                Controls.Add(edit);
                // need to support <%# %> expressions
                edit.DataBind();
            }
            if (ProgressIndicatorTemplate != null)
            {
                // create instance of DataViewItem control
                DataViewItem progres = new DataViewItem(DataSource, count++);
                // instantiate in new item object
                progres.ID = "ProgressTemplate";
                progres.IsClientTemplate = false;
                ProgressIndicatorTemplate.InstantiateIn(progres);
                if (RenderTable)
                {
                    progres.TemplateContainerTag = HtmlTextWriterTag.Tbody;
                }
                else
                {
                    progres.TemplateContainerTag = HtmlTextWriterTag.Div;
                }
                // add item to Controls collection
                Controls.Add(progres);
                // need to support <%# %> expressions
                progres.DataBind();
            }

        }

        protected override System.Collections.Generic.IEnumerable<System.Web.UI.ScriptDescriptor> GetScriptDescriptors()
        {
            string dataVewId = String.Format("{0}{1}", ClientID, (ItemTemplate != null ? "_ItemTemplate" : ""));
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Batik.UI.DataView", dataVewId);
            descriptor.AddProperty("dataProvider", DataProvider);
            descriptor.AddProperty("fetchOperation", FetchOperation);
            descriptor.AddProperty("fetchParameters", FetchParameters);
            descriptor.AddProperty("autoFetch", AutoFetch);
            descriptor.AddProperty("id", ClientID);
            descriptor.AddElementProperty("itemTemplate", String.Format("{0}_ItemTemplate", this.ClientID));
            descriptor.AddElementProperty("editTemplate", String.Format("{0}_EditTemplate", this.ClientID));
            descriptor.AddElementProperty("progressTemplate", String.Format("{0}_ProgressTemplate", this.ClientID));
            descriptor.AddProperty("datasourceID", DataSourceID);
            descriptor.AddProperty("selectedItemClass", "selected-row");
            yield return descriptor;
        }
        protected override System.Collections.Generic.IEnumerable<System.Web.UI.ScriptReference> GetScriptReferences()
        {
            yield return new ScriptReference("Batik.Web.UI.Controls.DataView.Script.js", this.GetType().Assembly.FullName);
        }
    }
}
